package Tracker.server.control;

import org.json.JSONException;
import org.json.JSONObject;

import Tracker.server.model.Session;
import Tracker.server.model.TError;

public class SessionController {
	ProtocolHandler handler;
	
	public SessionController(ProtocolHandler h) {
		this.handler = h;
	}
	
	public void get(JSONObject req, JSONObject res) throws JSONException {
		try {
			JSONObject sessionReq = req.getJSONObject("session");
			
			if(!sessionReq.isNull("id")) {
				if(this.handler.model.containsSession(sessionReq.getInt("id"))) {
					Session session =  this.handler.model.getSession(sessionReq.getInt("id"));
					res.append("success", new Boolean(true));
					res.append("session", session);
				} else {
					res.append("success", new Boolean(false));
					res.append("error", new TError("Not a valid sessison", 401));
					return;
				}
			}
			
		} catch (JSONException e) {
			res.append("success", new Boolean(false));
			res.append("error", new TError("No session object", 400));
			return;
		}
	}
}
